﻿using System;
using System.Data;
using Pantry.DataAccess;

namespace Pantry.Business
{
    public class PantryBus : IDisposable
    {
        protected PantryDao PantryDao { get; set; }

        public PantryBus()
        {
            PantryDao = new PantryDao();
        }
             
        public int CheckUnique(string table, string fieldName, string newValue)
        {
            if (string.IsNullOrEmpty(table) || string.IsNullOrEmpty(fieldName) || string.IsNullOrEmpty(newValue))
            {
                return 0;
            }

            return PantryDao.CheckUnique(table, fieldName, newValue);
        }

        public int CheckUniqueForAmend(string table, string fieldName, string newValue, string fieldId, int valueId)
        {
            if (string.IsNullOrEmpty(table) || string.IsNullOrEmpty(fieldName) || string.IsNullOrEmpty(newValue) || string.IsNullOrEmpty(fieldId) ||(valueId<1))
            {
                return 0;
            }

            return PantryDao.CheckUniqueForAmend(table, fieldName, newValue,fieldId,valueId);
        }

        public int ChangeIsActive(string table,string fieldName,string newValue,string fieldId,int valueId)
        {
            if (string.IsNullOrEmpty(table)||string.IsNullOrEmpty(fieldName)||string.IsNullOrEmpty(newValue)||string.IsNullOrEmpty(fieldId))
            {
                return 0;
            }

            return PantryDao.ChangeIsActive(table, fieldName, newValue, fieldId, valueId);
        }

        public DataTable RetrieveData(string conditionField,string conditionValue,SqlDbType sqlType)
        {
            if (string.IsNullOrEmpty(conditionField)||string.IsNullOrEmpty(conditionValue))
            {
                return null;
            }

            return PantryDao.RetrieveData(conditionField,conditionValue,sqlType);
        }

        public DataTable RetrieveAllData()
        {
            return PantryDao.RetrieveAllData();
        }

        public object RetrieveEntity(int id)
        {
            if (id < 1)
            {
                return null;
            }

            var entity = PantryDao.RetrieveEntity(id);
            return entity;
        }

        public int Insert(object sender)
        {
            if (sender == null)
            {
                return -1;
            }
            return PantryDao.Insert(sender);
        }

        public int Update(object sender)
        {
            if (sender == null)
            {
                return -1;
            }
            return PantryDao.Update(sender);
        }

        public int Delete(int id)
        {
            return id < 1 ? 0 : PantryDao.Delete(id);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                PantryDao.Dispose();
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
